using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ShippersEx : System.Web.UI.Page
{
    ShipperDAO daoShipper = new ShipperDAO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!(bool)(Session["logedin"])) Response.Redirect("Login.aspx");
        if (!IsPostBack) LoadData();
    }
    protected void dgrShippers_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = int.Parse(dgrShippers.DataKeys[e.RowIndex].Value.ToString());
        string companyName = ((TextBox)dgrShippers.Rows[e.RowIndex].FindControl("txtCompanyName")).Text;
        string  phone = ((TextBox)dgrShippers.Rows[e.RowIndex].FindControl("txtPhone")).Text;
        ShipperInfo sh = new ShipperInfo(); 
        sh.ID = id;
        sh.CompanyName = companyName;
        sh.Phone = phone;
        dgrShippers.EditIndex = -1;
        if (daoShipper.Update(sh) > 0)
        {
            LoadData();
        }
    }
    protected void dgrShippers_RowEditing(object sender, GridViewEditEventArgs e)
    {
        dgrShippers.EditIndex = e.NewEditIndex;
        LoadData();
        
    }
    protected void dgrShippers_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(dgrShippers.DataKeys[e.RowIndex].Value.ToString());
        ShipperInfo sh=new ShipperInfo();
        sh.ID=id;
        dgrShippers.EditIndex = -1;
        if(daoShipper.Delete(sh)>0){
            LoadData();
        }
    }
    protected void dgrShippers_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        dgrShippers.EditIndex = -1;
        LoadData();
    }
    public void LoadData()
    {
        dgrShippers.DataSource = daoShipper.GetAll();
        dgrShippers.DataBind();
    }
    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in dgrShippers.Rows)
        {
            bool check = ((CheckBox)row.FindControl("chkChecked")).Checked;
            ShipperInfo sh = new ShipperInfo();
            sh.ID = int.Parse(dgrShippers.DataKeys[row.RowIndex].Value.ToString());
            if (check) daoShipper.Delete(sh);
        }
        LoadData();
    }
    protected void dgrShippers_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        
    }
    public void onCheckedChange(object sender,EventArgs e)
    {
        if (((CheckBox)sender).Checked)
            foreach (GridViewRow row in dgrShippers.Rows)
            {
                ((CheckBox)row.FindControl("chkChecked")).Checked = true;
            }
        else
        {
            foreach (GridViewRow row in dgrShippers.Rows)
            {
                ((CheckBox)row.FindControl("chkChecked")).Checked = false;
            }
        }
    }

}
